const app = getApp()
const address = require('../../utils/city.js')
Page({
  data: {
    form: {
      id: '', // 积分商品ID，
      buyerId: '', // 用户ID，
      integral: '', // 积分，
      sellerId: '', // 代理商ID
      contacts: '', // 收货人/联系人
      phone: '', // 联系电话，
      invite: 0, // 是否自取   0  自取  1  邮寄
      areaInfo: '请选择地址',
      addressInfo: '', // 详细地址,
    },
    invite: [{
      text: '自取',
      value: 0,
      checked: true
    }, {
      text: '邮寄',
      value: 1
    }],
    msg: {
      contacts: '请输入联系人',
      phone: '请输入联系电话',
      areaInfo: '请选择地址',
      addressInfo: '请输入详细地址'
    },
    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    city: '',
    area: '',
    areaInfo: '请选择地址'
  },
  onLoad(e) {
    this.setData({
      e: e
    })

    let userId = wx.getStorageSync('userId')
    let agent = wx.getStorageSync('agent')
    let integral = wx.getStorageSync('integral')
    this.setData({
      'form.id': integral.id,
      'form.integral': integral.coinValues,
      'form.buyerId': userId,
      'form.sellerId': agent.id,
      integral
    })

    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation
    // 默认联动显示北京
    var id = address.provinces[0].id
    this.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id]
    })
  },
  onShow() {},
  // addOrder() { // 确认下单
  //   let { form, msg } = this.data
  //   for (var i in form) {
  //     if (form.invite) {
  //       if (form[i] === '' || form[i] === '请选择地址') {
  //         wx.showToast({
  //           title: msg[i],
  //           image: '../../../assets/images/warning.png',
  //           duration: 1500
  //         })
  //         return false
  //       }
  //     } else {
  //       if ((i !== 'areaInfo' && i !== 'addressInfo') && form[i] === '') {
  //         wx.showToast({
  //           title: msg[i],
  //           image: '../../../assets/images/warning.png',
  //           duration: 1500
  //         })
  //         return false
  //       }
  //     }
  //   }
  //   if (form.invite) {
  //     form.address = form.areaInfo + ' ' + form.addressInfo
  //   }
  //   App.HttpService.intSave(form).then(res => {
  //     let data = res.data
  //     if (data.code == 200) {
  //       wx.showToast({
  //         title: '兑换成功',
  //         icon: 'success',
  //         duration: 1500
  //       })
  //       wx.navigateTo({
  //         url: '/pages/integral/list/index?back=1'
  //       })
  //       wx.removeStorageSync('integral')
  //     } else {
  //       wx.showModal({
  //         title: '提示',
  //         content: data.msg,
  //         showCancel: false
  //       })
  //     }
  //   })
  // },
  clear() {
    app.HttpService.clearCartByUser().then(res => {
      const data = res.data
      console.log(data)
    })
  },
  inputBlur(e) { // 失去焦点时间
    let form = this.data.form
    let {
      key
    } = e.currentTarget.dataset
    let {
      value
    } = e.detail
    for (var i in form) {
      if (i == key) {
        this.setData({
          ['form.' + key]: value
        })
      }
    }
  },
  radioChange(e) {
    this.setData({
      'form.invite': parseInt(e.detail.value)
    })
  },
  selectDistrict(e) {
    var that = this
    if (that.data.addressMenuIsShow) { // 如果已经显示，不在执行显示动画
      return
    }
    that.startAddressAnimation(true) // 执行显示动画
  },
  startAddressAnimation(isShow) { // 执行动画
    var that = this
    if (isShow) {
      // vh是用来表示尺寸的单位，高度全屏是100vh
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  cityCancel(e) { // 点击地区选择取消按钮
    this.startAddressAnimation(false)
  },
  citySure(e) { // 点击地区选择确定按钮
    var that = this
    var city = that.data.city
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + ' ' + that.data.citys[value[1]].name + ' ' + that.data.areas[value[2]].name
    that.setData({
      'form.areaInfo': areaInfo
    })
  },
  hideCitySelected(e) { // 点击蒙版时取消组件的显示
    this.startAddressAnimation(false)
  },
  cityChange(e) { // 处理省市县联动逻辑
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    // 如果省份选择项和之前不一样，表示滑动了省份，此时市默认是省的第一组数据，
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      // 滑动选择了第二项数据，即市，此时区显示省市对应的第一组数据
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else { // 滑动选择了区
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  },
  addfrom(e) {
    console.log('form', e)
    var that = this
    var formid = e.detail.formId
    let val = e.detail.value
    console.log(formid)
    this.fid(formid);
  },

  // 传送fromid
  fid: function(e) {
    // fromid
    let uid = wx.getStorageSync('userId')
    app.HttpService.fid({
      buyerId: uid,
      fromId: e
    }).then(res => {
      console.log(res)
    })

  },
  lianxifangshi: function(e) {


    this.setData({
      lianxifangshi: e.detail.value
    })

  },
  lianxiren: function(e) {


    this.setData({
      lianxiren: e.detail.value
    })

  },
  xiangxidizhi: function(e) {
    this.setData({
      xiangxidizhi: e.detail.value
    })
  },

  beizhu: function(e) {
    this.setData({
      beizhu: e.detail.value
    })
  },


  shoujihao: function(e) {
    this.setData({
      shoujihao: e.detail.value
    })
  },

  //实物领取
  lingqu: function() {
    var t = this.data

    if (t.lianxiren && t.lianxifangshi) {

      if (!(/^1[3456789]\d{9}$/.test(t.lianxifangshi))) {
        wx.showModal({
          title: '温馨提示',
          content: '请输入11位正确手机号码',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return false;
      }

      if (t.form.invite == 1) {


        if (t.xiangxidizhi) {


        } else {

          wx.showModal({
            title: '温馨提示',
            content: '详细地址不能为空',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')

              }
            }
          })
          return false;
        }
      }
    } else {
      wx.showModal({
        title: '温馨提示',
        content: '联系信息不能为空',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')

          }
        }
      })
    }
    // var that = this
   
    if (t.form.invite == 1){
      var status = 2
    }else{
      var status=1
    }

    var params = {}
    params.id=t.e.id
    params.status = status
     params.mome= t.beizhu 
    params.name =t.lianxiren
    params.phone = t.lianxifangshi
    params.address = t.form.areaInfo + t.xiangxidizhi


    app.HttpService.lingjingqingqiu(params).then(res => {
      console.log("领奖请求", res)
      if (res.data.code==200){
        wx.showModal({
          title: '温馨提示',
          content: '领取成功',
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 1,
              })


            } else if (res.cancel) {
              console.log('用户点击取消')
              wx.navigateBack({
                delta: 1,
              })

            }
          }
        })
      }else{
        wx.showModal({
          title: '温馨提示',
          content: res.data.msg,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 1,
              })


            } else if (res.cancel) {
              console.log('用户点击取消')
              wx.navigateBack({
                delta: 1,
              })

            }
          }
        })

      }
    
    })


    console.log("发送请求", t)
  },
//点击充值
  chongzhi:function(){
    var t= this.data
    if (!(/^1[3456789]\d{9}$/.test(t.shoujihao))) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入11位正确手机号码',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }else{
     

      var params = {}
      params.id = t.e.id
  
      params.mome = t.beizhu
      params.status=1

      params.phone = t.shoujihao



      app.HttpService.lingjingqingqiu(params).then(res => {
        console.log("手机充值请", res)
        if (res.data.code == 200) {
          wx.showModal({
            title: '温馨提示',
            content: '充值成功',
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack({
                  delta: 1,
                })


              } else if (res.cancel) {
                console.log('用户点击取消')
                wx.navigateBack({
                  delta: 1,
                })

              }
            }
          })

        }else{


          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            success(res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx.navigateBack({
                  delta: 1,
                })


              } else if (res.cancel) {
                console.log('用户点击取消')
                wx.navigateBack({
                  delta: 1,
                })

              }
            }
          })



        }

      })



    }
    

    


  },
})